import { Component, OnInit } from '@angular/core';
import {AddressService} from '../../lib/service/address.service';
import {HttpService} from '../../lib/service/http.service';
import {adSystemApiUrl} from 'config/api';

export class AddContact {
  fullname: string;
  mobile: number;
  qq: number;
  province = 0;
  city = 0;
  type = '意向代理';
}

@Component({
  selector: 'app-partner',
  templateUrl: './partner.component.html',
  styleUrls: ['./partner.component.scss'],
  providers: [AddressService]
})
export class PartnerComponent implements OnInit {

  type: string;
  message: string;
  isShow = false;

  provinceList = [];
  cityList = [];
  productNum = 0;
  addContact = new AddContact;

  constructor(private addressService: AddressService,
              private httpService: HttpService) { }

  ngOnInit() {
    this.productCirculation();
    this.getProvinceList();
  }

  // 获取省列表
  getProvinceList(){
    this.addressService.getProvinceList()
      .subscribe(
        res => {
          this.provinceList = res.data;
        }
      )
  }

  // 更改省之后
  changeProvince(){
    this.addContact.city = 0;
    this.addressService.getCityList(this.addContact.province).subscribe(
      res => {
        this.cityList = res.data;
      }
    )
  }

  productCirculation() {
    setInterval(() => {
      this.productNum += 1;
      if (this.productNum > 4) {
        this.productNum = 0;
      }
    }, 2000);
  }

  // 代理商提交联系方式
  contactSubmit() {
    if (!this.addContact.city) {
      this.type = 'info';
      this.message = '请选择您的所在地';
      this.isShow = true;
      return;
    }
    const url = `${adSystemApiUrl}/v1/fddata/applet_apply/add`;
    return this.httpService.httpPostAuth(url,this.addContact).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '提交成功';
          this.isShow = true;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    )
  }
}
